package Modelo;

import RMI.SRMI;
import RMI.MissingConfigurationParameterException;
import VO.VOMensaje;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Servidor extends UnicastRemoteObject implements IServidor{
    private Vector clientes;

    public Servidor() throws RemoteException {
        this.clientes = new Vector();
    }

    public int pidePuerto() throws RemoteException{
        System.out.println("solicitando puerto: "+(3233+this.clientes.size()));
        return 3233+this.clientes.size();
    }
    
    public int login(ICliente c, int p, String user) throws RemoteException {
        if(this.checkUser(user)==0) {
            try {
                System.out.println("solicitando registro de: "+user);
                Vector cli = new Vector(2);
                cli.add(user);
                cli.add(SRMI.getCliente(c, p));
                this.clientes.add(cli);
                System.out.println("Num Clientes: "+this.clientes.size());
                return 0;
            } catch (MissingConfigurationParameterException ex) {
                Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
                return 1;
            } catch (NotBoundException ex) {
                Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
                return 1;
            } catch (MalformedURLException ex) {
                Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
                return 1;
            } catch (RemoteException ex) {
                Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
                return 1;
            }
        }
        return 2;
    }

    public int enviar(VOMensaje m) throws RemoteException {
        System.out.println(m.getEmisor()+"->"+m.getReceptor()+": "+m.getMsj());
        for(int i=0; i<this.clientes.size(); i++) {
            System.out.println(m.getEmisor()+"/"+((Vector)this.clientes.get(i)).get(0).toString());
            if(((Vector)this.clientes.get(i)).get(0).toString().compareTo(m.getReceptor())==0) {
                ((ICliente)((Vector)(this.clientes.get(i))).get(1)).recibir(m);
                System.out.println("Enviado a: "+((Vector)this.clientes.get(i)).get(0).toString());
                return 0;
            }
        }
        return 1;
    }

    public Vector getUserContectados() throws RemoteException {
        return this.clientes;
    }

    public int logout(String user) throws RemoteException {
        System.out.println("el cliente "+user+" quiere cerrar sesion");
        for(int i=0; i<this.clientes.size(); i++) {
            if(((Vector)this.clientes.get(i)).get(0).toString().compareTo(user)==0) {
                this.clientes.remove(i);
                System.out.println("el cliente "+user+" cerro sesion");
                return 0;
            }
        }
        return 1;
    }

    public int checkUser(String user) {
        int r=0;
        for(int i=0; i<this.clientes.size(); i++)
            if(((Vector)this.clientes.get(i)).get(0).toString().compareTo(user)==0) {
                r++;
                return r;
            }
        return r;
    }
}
